home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 3.5 KB | 84 lines | [TEXT/GEOL] |
- Item 6435527 24-Aug-90 05:32PDT
-
- From: POWERUP.ENG Power Up Software,PRT
-
- To: MACAPP.TECH$ MacApp Technical
-
- Sub: Multiple Inheritance Ques
-
- Attn: MacApp.Tech$
- SentBy: James Plamondon
- Date 8/23/90
- Subject Multiple Inheritance Questi
- From James Plamondon
- To MacApp.Tech$
-
- Subject: Multiple Inheritance Question
- Gentlepersons,
-
- I've been asked more than once for an example of a situation in which multiple
- inheritance is really useful, rather than being a means of avoiding thoughtful
- design. I think I've come up with one, and I'd like to hear the comments of
- anyone interested in responding.
-
- Consider a application which allows the user to play draw poker against the
- computer.
-
- Each card can be in any one of four states at any given time:
- • neither player can see it at all
- • both players can see its back
- • both players can see its face
- • one player can see only its back, while the other can see only its face
-
- Let's assume that both players are trying to cheat, by marking the backs of
- some of the cards. Then, viewing the back of the card might give the viewer
- some valuable information about the card (although not as much as could be
- gained by viewing its face). Each player would have a list of references to
- the cards in her hand, and to those in her opponent's hand. Each player would
- look at only the faces of her own cards, and only the backs of her opponent's
- cards, while both could examine the back of the card on the top of the deck.
-
- The question is — how to represent a card? One way to represent the cards
- would be to have a single class, TCard, which would contain a description of
- both a card's front and its back.
-
- But — what would stop the program from cheating even more, and looking at the
- faces of its opponent's cards? A TCard is a TCard is a TCard — and if the
- computer can access the face data of its own TCards, then it can access the
- face data of its opponent's TCards, too. Declaring the face data members
- "private" wouldn't help — then the computer couldn't see the faces of its own
- cards, either.
-
- If, faced with this problem, we chose to redesign the program so that each
- card would be represented by two different objects — its back and its face —
- then we would have introduced a significant source of potential errors: the
- two objects could get disassociated, so that the back of one card would be
- associated with the face of another.
-
- Here's my proposed solution. Use two classes, TCardFace and TCardBack, to
- describe the face and back of a card, respectively. Then, have a third class,
- TCard, inherit from both, allowing the complete description of a card within a
- single TCard object.
-
- Each card in the game would be described by a single TCard object. Each
- player's own cards would be kept in a list of objects of type TCardFace, while
- her list of her opponents cards would be refered to as objects of type
- TCardBack. Thus, each player would have access to only that portion of the
- card's representation that was appropriate, while still retaining the
- advantages of having each card be represented by a single object.
-
- Of course, for every defense, there's an offense: a player could always cast
- the reference to the opponent's card to a TCard, and then cheat to her
- heart's content.
-
- And so I ask — is this an elegant solution to a thorny problem, or just
- another example of the kind of weird, convoluted kludge for which I've become
- infamous?
-
- Looking forward to your responses, I remain
-
- Yours,
-
- James Plamondon
-
-